package com.xgkj.diyiketang.util.xiangce;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;

import com.xgkj.diyiketang.util.MyCustomAdapter;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;

/**
 * Created by windo on 16/4/13.
 */
public class PhotoImageAdapter extends MyCustomAdapter{
    private String TAG = getClass().getSimpleName();
    private Context mContext;
    private ArrayList<PhotoImageInfo> imageList = new ArrayList<PhotoImageInfo>();
    private ArrayList<PhotoImageInfo> selectImageList = new ArrayList<PhotoImageInfo>();
    private BitmapCache imageLoadCache;

    BitmapCache.ImageCallback callback = new BitmapCache.ImageCallback() {
        @Override
        public void imageLoad(ImageView imageView, Bitmap bitmap,
                              Object... params) {
            if (imageView != null && bitmap != null) {
                String url = (String) params[0];
                if (url != null && url.equals((String) imageView.getTag())) {
                    ((ImageView) imageView).setImageBitmap(bitmap);
                } else {
                }
            } else {
            }
        }
    };

    public PhotoImageAdapter(Context context, AbsListView listView) {
        super(context, listView);
        mContext = context;
        imageLoadCache = new BitmapCache(mContext);
    }

    //    public PhotoImageAdapter(Context context) {
//        mContext = context;
//        imageLoadCache = new BitmapCache(context);
//    }

    public void updateData(ArrayList<PhotoImageInfo> allList,ArrayList<PhotoImageInfo> selectList){
        imageList = allList;
        selectImageList = selectList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (imageList != null){
            return imageList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (imageList != null){
            return imageList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.photoimage_item,parent,false);
            childHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
            childHolder.toggleButton = (ImageView) convertView.findViewById(R.id.toggle_button);
            childHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(childHolder);
        }else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        if (imageList != null && imageList.size() > position){
            PhotoImageInfo info = imageList.get(position);
            childHolder.imageView.setTag(info.getImagePath());
            if (isScrolling){
                Bitmap cachethmub = imageLoadCache.get(info.getThumbnailPath());
                Bitmap cacheBit = imageLoadCache.get(info.getImagePath());
                if (cachethmub != null){
                    childHolder.imageView.setImageBitmap(cachethmub);
                }else if (cacheBit != null){
                    childHolder.imageView.setImageBitmap(cacheBit);
                }else {
                    childHolder.imageView.setImageResource(R.drawable.zhanweitu);
                }
            }else {
                imageLoadCache.displayBmp(childHolder.imageView, info.getThumbnailPath(), info.getImagePath(), callback);
            }
            childHolder.checkBox.setTag(position);
            childHolder.checkBox.setOnCheckedChangeListener(new ToggleClickListener(childHolder.toggleButton));
            if (isContains(info)){
                childHolder.checkBox.setChecked(true);
            }else {
                childHolder.checkBox.setChecked(false);
            }
        }

        return convertView;
    }

    private boolean isContains(PhotoImageInfo info){
        for (int i=0;i<selectImageList.size();i++){
            PhotoImageInfo selectInfo = selectImageList.get(i);
            if (selectInfo.imageId == info.imageId){
                return true;
            }
        }
        return false;
    }

    class ChildHolder {
        public ImageView imageView;
        public ImageView toggleButton;
        public CheckBox checkBox;
    }

    private class ToggleClickListener implements CompoundButton.OnCheckedChangeListener {
        ImageView chooseBt;
        public ToggleClickListener(ImageView choosebt){
            this.chooseBt = choosebt;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (buttonView instanceof CheckBox) {
                CheckBox toggleButton = (CheckBox) buttonView;
                int position = (Integer) toggleButton.getTag();
                if (imageList != null && mOnItemClickListener != null
                        && position < imageList.size()) {
                    mOnItemClickListener.onItemClick(toggleButton, position, isChecked,chooseBt);
                }
            }
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    public interface OnItemClickListener {
        public void onItemClick(CheckBox view, int position,
                                boolean isChecked, ImageView chooseBt);
    }
}
